-- 运营活动: 充值好礼


--[[
{
    cmd_id = 1, -- 命令id，用于批量停止、删除活动 
    act_id = 68, -- 活动id (68 充值好礼)
    act_name = "充值好礼", -- 活动名称
    act_desc = "充TMD，好礼不拿白不拿", -- 活动描述
    beg_time = 1493262000,  -- 活动开始时间(unix timestamp)
    end_time = 1693268400,  -- 活动结束时间(unix timestamp)
    config = -- 配置数据
    {
        rechargeGoldNum = 300,  --充值元宝数量
        dailyReward =  --每日奖励物品列表
        {
            {   --第一天
                { type = 0, id = 1091, count = 1, bind = 1,},
            },
            {   --第二天
                { type = 0, id = 1091, count = 1, bind = 1,},
            },
            {   --第三天
                { type = 0, id = 1091, count = 1, bind = 1,},
            },
            {   --第四天
                { type = 0, id = 1091, count = 1, bind = 1,},
            },
            {   --第五天
                { type = 0, id = 1091, count = 1, bind = 1,},
            },
        },
    },
}
]]

-- 检查配置是否正确，正确则返回 0，否则返回错误码
function oper_act_68_check_cfg(cfg)
    if cfg == nil then return 20 end
    
    local config = cfg.config
    if config == nil then return 21 end
    
    if config.dailyReward == nil then return 23 end
    if type(config.dailyReward) ~= "table" then return 24 end
    for idx = 1, #config.dailyReward do
        local item = config.dailyReward[idx]
        if not oper_act_check_one_award_cfg(item) then
            return 22
        end 
    end

    return 0
end

-- 安装活动，成功则返回 0，否则返回错误码
function oper_act_68_setup(cfg) 
    return 0
end

-- 卸载活动
function oper_act_68_unsetup(cfg)   
end

-- 活动开始
function oper_act_68_start(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_68_start = 1
    ssVar.oper_act_68_cmd = cfg.cmd_id
    ssVar.oper_act_68_act = cfg.act_id

    local data = gRankMgr_GetValueByIndx(0, RANK_DEFINE_OPERATION_68, 1)

    local needClearState = true

    local crossDayState = false

    local time = System.getSystemTime()

    if data then
        local myData = BaseFuc_unserialize(data)
        if myData and myData.cmd_id and myData.cmd_id == cfg.cmd_id and myData.act_id and myData.act_id == cfg.act_id and
            myData.time and cfg.beg_time <= myData.time and  cfg.end_time > myData.time then
            needClearState = false
            
            if myData and myData.mySelfTime and not System.isUnixTimeSameDay(myData.mySelfTime, time) then
                crossDayState = true
            end
        end
    end

    if crossDayState then
        if cfg then
            local config = cfg.config
            if config then 
                local rankPtr = Ranking.getRanking(RankList[RANK_DEFINE_OPERATION_68].strName)
                local rankCount = Ranking.getRankItemCount(rankPtr)
                for idx = 1, rankCount do
                    local item = Ranking.getItemFromIndex(rankPtr, idx-1)
                    if item then
                        local actorid = tonumber(Ranking.getId(item))
                        local data = Ranking.getSub(item, 1)
                        local dataTable
                        if not data or data == "" then
                            dataTable = {}
                        else
                            dataTable = BaseFuc_unserialize(data)
                        end

                        local nIndex = oper_act_68_get_day_by_cfg(cfg) - 1

                        local myRewards = config.dailyReward[nIndex]    

                        if dataTable.myRechargeState and myRewards then
                            if not dataTable.myRewardsState then
                                dataTable.myRewardsState = {}
                            end 
                            if not dataTable.myRewardsState[nIndex] or dataTable.myRewardsState[nIndex] ~= 2 then
                                dataTable.myRewardsState[nIndex] = 2
                                dataTable.mySelfTime = time
                                local title = Lang.ScriptTips.Operation188
                                local content = Lang.ScriptTips.Operation189

                                local newData = BaseFuc_serialize(dataTable)
                                gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_68, 1, newData)

                                Awards.GiveToMailById(actorid, myRewards, title, content, LogIds.OperationLogId068, Lang.LogTips.Operation068, dataTable.vocation, dataTable.sex)
                            end
                        end
                    end
                end
            end
        end
    end

    if needClearState then
        gRankMgr_Clear(RANK_DEFINE_OPERATION_68)
    end
end

-- 活动结束
function oper_act_68_end(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_68_start = nil
    ssVar.oper_act_68_cmd = nil
    ssVar.oper_act_68_act = nil
end

-- 打包数据
function oper_act_68_packet_data(sysarg, cfg)

    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_68_start then
        return
    end
    if not cfg then
        return
    end

    local config = cfg.config
    if not config then
        return
    end

    
    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_68, 1)
    local myData = BaseFuc_unserialize(data)

    if not myData or myData == "" or myData == 0 then
        myData = {}
    end

    if not myData.myRechargeNum then
        myData.myRechargeNum = 0
    end

    local nIndex = oper_act_68_get_day_by_cfg(cfg)

    if not myData.myRewardsState then
        myData.myRewardsState = {}
    end

    local pack = oper_act_alloc_init_data_packet(sysarg, cfg.cmd_id, cfg.act_id)
    if (pack == nil) then
        return
    end
    DataPack.writeInt(pack, myData.myRechargeNum)
    local myRechargeState = myData.myRechargeState or 0
    DataPack.writeByte(pack, myRechargeState)
    DataPack.writeByte(pack, nIndex)
    local len = #config.dailyReward
    DataPack.writeByte(pack, len)
    for idx = 1, len do
        DataPack.writeByte(pack, idx)
        local state = myData.myRewardsState[idx] or 0
        if myRechargeState > 0 and state == 0  and nIndex == idx then
            state = 1
        end
        DataPack.writeByte(pack, state)
    end
    DataPack.flush(pack)
    --DataPack.writeByte(netPack, 1)        -- 活动特有参数
end


-- 操作，返回操作结果，0为成功
function oper_act_68_do(sysarg, cfg, clientPkt)
    if cfg == nil then return end
    
    if cfg.state == nil or cfg.state ~= 1 then -- 不在活动中
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation001, ttFlyTip)
        return
    end
    
    local config = cfg.config
    if config == nil then -- 参数错误
        print("error, oper_act_68_do cfg.config confg error")
        return
    end


    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_68, 1)

    local myData = BaseFuc_unserialize(data)
    

    if not myData or myData == "" or myData == 0 then
        myData = {}
    end
  
    if not myData.myRechargeState then
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation185, ttFlyTip)
        return
    end

    local nIndex = oper_act_68_get_day_by_cfg(cfg)

    local myRewards = config.dailyReward[nIndex]     

    if not myRewards then
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation186, ttFlyTip)
        return
    end

    if not myData.myRewardsState then
        myData.myRewardsState = {}
    end

    if myData.myRewardsState[nIndex] and myData.myRewardsState[nIndex] == 2 then
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation187, ttFlyTip)
        return
    end
    local time = System.getSystemTime()
    if IsBagGridEnough(sysarg, myRewards) then

        myData.myRewardsState[nIndex] = 2

        myData.mySelfTime = time

        GiveCommonAward(sysarg, myRewards, LogIds.OperationLogId068, Lang.LogTips.Operation068)

        if not myData.cmd_id then
            myData.cmd_id = cfg.cmd_id
            myData.act_id = cfg.act_id
            myData.time = time
        end

        if not myData.sex then
            myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
            myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
        end

        local newData = BaseFuc_serialize(myData)

        gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_68, 1, newData)


        local netPack = oper_act_alloc_do_result_packet(sysarg, cfg.cmd_id, cfg.act_id, result)
        DataPack.writeByte(netPack, nIndex)
        DataPack.writeByte(netPack, 2)
        DataPack.flush(netPack)
    else
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.COM0015, ttFlyTip)
        return
    end
end



-- 跨天
function oper_act_68_new_day_arrive(act_id)  
    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_68_start then
        return
    end
    if not ssVar.oper_act_68_cmd then
        return
    end
    if not ssVar.oper_act_68_act then
        return
    end
    
    local cfg = get_oper_act_cfg(ssVar.oper_act_68_cmd, ssVar.oper_act_68_act)
    if not cfg then
        return
    end

    local config = cfg.config
    if not config then
        return
    end 

    local time = System.getSystemTime()


    local rankPtr = Ranking.getRanking(RankList[RANK_DEFINE_OPERATION_68].strName)
    local rankCount = Ranking.getRankItemCount(rankPtr)
    for idx = 1, rankCount do
        local item = Ranking.getItemFromIndex(rankPtr, idx-1)
        if item then
            local actorid = tonumber(Ranking.getId(item))
            local data = Ranking.getSub(item, 1)
            local dataTable
            if not data or data == "" then
                dataTable = {}
            else
                dataTable = BaseFuc_unserialize(data)
            end

            local nIndex = oper_act_68_get_day_by_cfg(cfg) - 1

            local myRewards = config.dailyReward[nIndex]    

            if dataTable.myRechargeState and myRewards then
                if not dataTable.myRewardsState then
                    dataTable.myRewardsState = {}
                end 
                if not dataTable.myRewardsState[nIndex] or dataTable.myRewardsState[nIndex] ~= 2 then
                    dataTable.myRewardsState[nIndex] = 2
                    dataTable.mySelfTime = time
                    local title = Lang.ScriptTips.Operation188
                    local content = Lang.ScriptTips.Operation189

                    local newData = BaseFuc_serialize(dataTable)
                    gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_68, 1, newData)

                    Awards.GiveToMailById(actorid, myRewards, title, content, LogIds.OperationLogId068, Lang.LogTips.Operation068, dataTable.vocation, dataTable.sex)
                end
            end
          
            if dataTable.myRechargeState  then
                local sysarg = Actor.getActorById(actorid)
                if sysarg then
                    oper_act_68_packet_data(sysarg, cfg)        
                end
            end
        end
    end
end


function oper_act_68_withdrawyuanbao(sysarg, nYuanBao)
    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_68_start then
        return
    end
    if not ssVar.oper_act_68_cmd then
        return
    end
    if not ssVar.oper_act_68_act then
        return
    end
    local cfg = get_oper_act_cfg(ssVar.oper_act_68_cmd, ssVar.oper_act_68_act)
    if not cfg then
        return
    end
    local config = cfg.config
    if not config then
        return
    end
    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_68, 1)

    local time = System.getSystemTime()
    if not System.isUnixTimeSameDay(cfg.beg_time, time) then
        return
    end

    local myData = BaseFuc_unserialize(data)

    if not myData then
        myData = {}
    end

    if not myData.myRechargeNum then
        myData.myRechargeNum = 0
    end

    myData.myRechargeNum = myData.myRechargeNum + nYuanBao

    if not myData.myRechargeState and myData.myRechargeNum >= config.rechargeGoldNum then
        myData.myRechargeState = 1
    end

    if not myData.cmd_id then
        myData.cmd_id = cfg.cmd_id
        myData.act_id = cfg.act_id
        myData.time = time
    end

    if not myData.sex then
        myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
    end


    local newData = BaseFuc_serialize(myData)
    gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_68, 1, newData)

    oper_act_68_packet_data(sysarg, cfg)
end


function oper_act_68_get_day_by_cfg(cfg)
    local index = 1
    local time = System.getSystemTime()
    local second = System.getCurrMiniTime() - System.getToday()
    local zeroTime = time - second
    if zeroTime > cfg.beg_time then
        index = math.floor((zeroTime - cfg.beg_time) / 86400) + 1
        if (zeroTime - cfg.beg_time) % 86400 ~= 0 then
            index = index + 1
        end
    end

    return index
end





